package com.hutra.bitmap {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.IBitmapDrawable;
	import flash.display.MovieClip;
	import flash.display.PixelSnapping;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	/**
	 * ...
	 * @author hunglx.usth
	 */
	public class MovieBitcrop {
		
		public function MovieBitcrop() {
		
		}
		
		/**
		 * Move
		 * Center point(0,0)
		 * |----------------|
		 * |	0.0			|
		 * |				|
		 * |		.---------------.
		 * |		|(80,80,100,90)	|
		 * |		|	Object		|
		 * |		|				|
		 * |		.---------------.
		 * |----------------|
		 *
		 * to
		 *
		 * .0.0------------.|
		 * |			   ||
		 * |	Object	   ||
		 * |			   ||
		 * .---------------.|
		 * |----------------|
		 *
		 * @param	a
		 * @param	b
		 * @return
		 */
		private function movePosition(a:Number, b:Number):Number {
			// -25 0 5 =  30 [1]
			// -25 0 -5 = 20 [2]
			//  5  0 25 = 20 [3]
			if (a > 0 && b > 0)
				return b - a; //[3]
			if (a < 0 && b < 0)
				return Math.abs(a) - Math.abs(b); //[2]
			if (a < 0 && b > 0)
				return Math.abs(a) + b; //[1]
			return 0;
		}
		
		/**
		 * [1],[2],[3] Scaled
		 * @param	a
		 * @param	b
		 * @param	c
		 * @return BitmapData
		 */
		public function getBaseBitmapData(a:*, b:Rectangle, c:Boolean):BitmapData {
			//[1]
			var f:Rectangle = a.getBounds(a);
			var g:Matrix = new Matrix(1, 0, 0, 1, -f.x, -f.y);
			g.scale(a.scaleX, a.scaleX);
			
			var wi:Number = dePercent(a.scaleX * 100, 0, f.width); //[2]
			var hi:Number = dePercent(a.scaleX * 100, 0, f.height); //[3]
			//[4]
			var h:BitmapData = new BitmapData(b.width, b.height, true, 0x00FFFFFF);
			h.draw(a, g, null, null, null, c);
			//[5]
			var xpo:Number = movePosition(b.x, f.x);
			var ypo:Number = movePosition(b.y, f.y);
			//[6]	
			var k:BitmapData = new BitmapData(b.width, b.height, true, 0x00FFFFFF);
			k.copyPixels(h, new Rectangle(0, 0, b.width, b.height), new Point(xpo, ypo));
			//[7]
			h.dispose();
			
			return k;
		}
		
		/**
		 * Function on lib: NumberUtilities.as.dePercent()
		 * Percentage change from the current number
		 * @see http://blog.hutrasoft.com/2011/10/16-useful-mathematical-formulas-in.html
		 * @param	percent
		 * @param	start
		 * @param	end
		 * @return number
		 */
		public static function dePercent(p:Number, s:Number = 0, e:Number = 100):Number {
			return (e - ((e - s) - ((p / 100) * (e - s))));
		}
	}

}